namespace iScadaFBD
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    [Serializable]
    public class Line
    {
        private System.Drawing.Color color;
        private Point endPoint;
        private Direction lineDirection;
        private bool movedAble;
        private Point startPoint;

        public Line(Point start_Point, Point end_Point)
        {
            this.startPoint = start_Point;
            this.endPoint = end_Point;
            if (Math.Abs((int) (start_Point.X - end_Point.X)) <= 1)
            {
                this.lineDirection = Direction.VERTICAL;
            }
            if (Math.Abs((int) (start_Point.Y - end_Point.Y)) <= 1)
            {
                this.lineDirection = Direction.HORIZONTAL;
            }
            this.Color = System.Drawing.Color.Black;
        }

        public void Draw(Graphics g)
        {
            Pen pen = new Pen(this.Color, 1f);
            g.DrawLine(pen, this.startPoint, this.endPoint);
            pen.Dispose();
        }

        public Cursor GetCursor(Point point)
        {
            if (this.movedAble)
            {
                switch (this.GetDirection())
                {
                    case Direction.VERTICAL:
                        return Cursors.SizeWE;

                    case Direction.HORIZONTAL:
                        return Cursors.SizeNS;
                }
            }
            return Cursors.Default;
        }

        public Direction GetDirection()
        {
            if (this.startPoint.Y == this.endPoint.Y)
            {
                return Direction.HORIZONTAL;
            }
            return Direction.VERTICAL;
        }

        public void Move(int deltaX, int deltaY)
        {
            if (this.movedAble)
            {
                if (this.lineDirection == Direction.HORIZONTAL)
                {
                    this.startPoint.Y += deltaY;
                }
                if (this.lineDirection == Direction.VERTICAL)
                {
                    this.startPoint.X += deltaX;
                }
            }
        }

        public bool PointNearLine(Point point)
        {
            Rectangle a = new Rectangle(this.startPoint.X, this.startPoint.Y, 0, 0);
            Rectangle b = new Rectangle(this.endPoint.X, this.endPoint.Y, 0, 0);
            Size size = new Size(3, 3);
            a.Inflate(size);
            b.Inflate(size);
            return Rectangle.Union(a, b).Contains(point);
        }

        public System.Drawing.Color Color
        {
            get
            {
                return this.color;
            }
            set
            {
                this.color = value;
            }
        }

        public Point EndPoint
        {
            get
            {
                return this.endPoint;
            }
            set
            {
                this.endPoint = value;
            }
        }

        public bool MovedAble
        {
            get
            {
                return this.movedAble;
            }
            set
            {
                this.movedAble = value;
            }
        }

        public Point StartPoint
        {
            get
            {
                return this.startPoint;
            }
            set
            {
                this.startPoint = value;
            }
        }

        public enum Direction
        {
            VERTICAL,
            HORIZONTAL
        }
    }
}

